package com.wind.activity;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.signature.ObjectKey;
import com.wind.beans.tokenBean;
import com.wind.soblogin.AppMd5Utils;
import com.wind.soblogin.HttpManager;
import com.wind.soblogin.LoginBody;
import com.debug.soblogin.R;
import com.wind.soblogin.SobUser;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    private EditText etPhone;
    private EditText etPwd;
    private EditText etCode;
    private Button btnLogin;
    private ImageView ivCode;
    private TextView tvResponse;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //TODO:登陆前和登陆后分别怎么做
        setContentView(R.layout.activity_main);
        initView();
        refreshCode();

        //请求验证码

    }


    private void initView() {
        etPhone = findViewById(R.id.et_account);
        etPwd = findViewById(R.id.et_pwd);
        etCode = findViewById(R.id.et_verify_code);
        btnLogin = findViewById(R.id.btn_login);
        ivCode = findViewById(R.id.verify_code_image);


        //点击图片刷新验证码
        ivCode.setOnClickListener(view -> refreshCode());


        //加密密码,提交接口
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //拿到et数据
                String phone = etPhone.getText().toString().trim();
                String pwd = etPwd.getText().toString().trim();
                String code = etCode.getText().toString().trim();
                System.out.println("code----->" + code);
                LoginBody body = new LoginBody(phone, AppMd5Utils.getMD5(pwd));
                System.out.println("棉麻是-------》" + AppMd5Utils.getMD5(pwd));
                HttpManager
                        .getSobApi()
                        .doLogin(code, body)
                        .enqueue(new Callback<SobUser>() {
                            @Override
                            public void onResponse(Call<SobUser> call, Response<SobUser> response) {

                                SobUser user = response.body();
                                System.out.println("user----->" + user.getMessage());
                                System.out.println("code----->" + user.getCode());
                                if (user.getCode() == 20000 && (user != null)) {
                                    //把登录信息保存下来
                                    SharedPreferences sp = getSharedPreferences("isLogin", Context.MODE_PRIVATE);
                                    SharedPreferences.Editor editor = sp.edit();
                                    String loginValue = "1";
                                    editor.putString("isLogin", loginValue);
                                    editor.commit();
                                    //从token里获取信息
                                    checkToken();
                                    Intent intent = new Intent(MainActivity.this, navActivity.class);
                                    startActivity(intent);
                                    Toast.makeText(MainActivity.this, "阁下登录成功啦", Toast.LENGTH_LONG).show();
                                } else {
                                    Toast.makeText(MainActivity.this, user.getMessage(), Toast.LENGTH_LONG).show();
                                }


                            }

                            @Override
                            public void onFailure(Call<SobUser> call, Throwable t) {
                                Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_LONG).show();
                            }
                        });
            }
        });
    }

    public void checkToken() {
        HttpManager
                .getSobApi()
                .checkToken()
                .enqueue(new Callback<tokenBean>() {
                    @Override
                    public void onResponse(Call<tokenBean> call, Response<tokenBean> response) {
                        tokenBean token = response.body();

                        //将头像和id，以及他的个性签名记录下来
                        SharedPreferences sp = getSharedPreferences("userInfo", Context.MODE_PRIVATE);
                        SharedPreferences.Editor editor = sp.edit();
                        assert token != null;
                        editor.putString("avatar", token.getData().getAvatar());
                        editor.putString("name", token.getData().getUserName());
                        editor.putString("sign", token.getData().getSign());
                        editor.commit();

                    }

                    @Override
                    public void onFailure(Call<tokenBean> call, Throwable t) {

                    }
                });

    }

    private void refreshCode() {
        //TODO:这里是glid加载同一个url图片不改变的解决办法

        System.out.println("执行了一次");

        RequestOptions userAvatarOptions = new RequestOptions().signature(new ObjectKey(System.currentTimeMillis()));
        //https://www.lolxiaoguo.cn/user/captcha/?random=555
        String x = "https://lolxiaoguo.cn/user/captcha";
        Glide.with(MainActivity.this).applyDefaultRequestOptions(userAvatarOptions).load(x).
                into(ivCode);


//        Glide.with(this).load(x).into(ivCode)
        ;
    }
}